import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from common.base import Base
from common.functions import open_browser



class AddressPage(Base):

    # 定位‘用户中心’和点击
    def click_user(self):
        self.click((By.LINK_TEXT, '用户中心'))

    # 定位‘收货地址’和点击
    def click_address(self):
        self.click((By.XPATH, '//div[@class="userMenu"]/a[4]'))

    # 定位’国家下拉框‘和选择
    def select_country(self):
        self.find_element((By.XPATH, '//select[@name="country"]/option[2]'))

    # 定位’省份‘和选择
    def select_province(self, index):
        s1 = self.find_element((By.ID, 'selProvinces_0'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位’市‘和选择
    def select_city(self,index):
        s1 = self.find_element((By.ID, 'selCities_0'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位区和选择
    def select_area(self, index):
        s1 = self.find_element((By.ID, 'selDistricts_0'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位收货人姓名和输入
    def input_name(self, name):
        self.find_element((By.ID, 'consignee_0')).send_keys(name)

    # 定位电子邮件地址和输入
    def input_email(self, email):
        self.find_element((By.ID, 'email_0')).send_keys(email)

    # 定位详细地市和输入
    def input_address(self, address):
        self.find_element((By.ID, 'address_0')).send_keys(address)

    # 定位邮政编码和输入
    def input_zipcode(self, zipcode):
        self.find_element((By.ID, 'zipcode_0')).send_keys(zipcode)

    # 定位电话和输入
    def input_tel(self, tel):
        self.find_element((By.ID, 'tel_0')).send_keys(tel)

    # 定位手机和输入
    def input_mobile(self, mobile):
        self.find_element((By.ID, 'mobile_0')).send_keys(mobile)

    # 定位确认新增收货地址
    def click_add(self):
        self.click((By.NAME, 'submit'))

    # 定位确认修改和点击
    def click_alter(self):
        self.click((By.XPATH, '//input[@value="确认修改"]'))

    # 定位删除和点击
    def click_del(self):
        self.click((By.NAME, 'button'))

    # 弹窗取消
    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()
        time.sleep(5)

    # 再定位删除和点击
    def click_del1(self):
        self.click((By.NAME, 'button'))
        time.sleep(5)

    #  弹窗确定
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accept()

# TODO 增加地址
# 定位’省份‘和选择
    def select_province1(self, index):
        s1 = self.find_element((By.ID, 'selProvinces_1'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位’市‘和选择
    def select_city1(self,index):
        s1 = self.find_element((By.ID, 'selCities_1'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位区和选择
    def select_area1(self, index):
        s1 = self.find_element((By.ID, 'selDistricts_1'))
        select = Select(s1)
        select.select_by_index(index)

    # 定位收货人姓名和输入
    def input_name1(self, name):
        self.find_element((By.ID, 'consignee_1')).send_keys(name)

    # 定位电子邮件地址和输入
    def input_email1(self, email):
        self.find_element((By.ID, 'email_1')).send_keys(email)

    # 定位详细地市和输入
    def input_address1(self, address):
        self.find_element((By.ID, 'address_1')).send_keys(address)

    # 定位邮政编码和输入
    def input_zipcode1(self, zipcode):
        self.find_element((By.ID, 'zipcode_1')).send_keys(zipcode)

    # 定位电话和输入
    def input_tel1(self, tel):
        self.find_element((By.ID, 'tel_1')).send_keys(tel)

    # 定位手机和输入
    def input_mobile1(self, mobile):
        self.find_element((By.ID, 'mobile_1')).send_keys(mobile)

    # 定位确认新增收货地址
    def click_add1(self):
        self.click((By.CLASS_NAME, 'bnt_blue_2'))


